﻿using UnityEngine;
using System.Collections;
using System;

/// Oppo广告
public class OppoAds : AdsBase
{
	#region  初始化数据

	public override void Init (params string[] key)
	{
		jo = new AndroidJavaObject ("com.oppo.ads.Main");
		jo.CallStatic ("init", key[0]);
		jo.CallStatic ("initBanner", key[1], (int)BannerPos.down, (int)BannerwWidth.wrap);
		jo.CallStatic ("initInsert", key[2]);
	}

	#endregion

	#region  展示Banner 插屏 原生

	public override void ShowBanner (Action<bool> ac)
	{
		TJLog ("展示Banner广告");
		bannerCallBack = ac;
		jo.CallStatic ("showBanner");
	}

	public override void CloseBanner ()
	{
		TJLog ("关闭Banner广告");
		jo.CallStatic ("closeBanner");
	}

//	public override void ShowNative (Action<bool> ac)
//	{
//		TJLog ("展示原生广告");
//		nativeCallBack = ac;
//		jo.CallStatic ("showNative");
//	}
//
//	public override void CloseNative ()
//	{
//		TJLog ("关闭原生广告");
//		jo.CallStatic ("closeNative");
//	}

	public override void ShowInsert (Action<bool> ac)
	{
		TJLog ("展示插屏广告");
		insertCallBack = ac;
		jo.CallStatic ("showInsert");
	}

	#endregion

	#region 判断Banner和插屏是否加载好

	public override bool IsBannerReady ()
	{	
		TJLog ("检测Banner状态");
		if (jo.CallStatic<bool> ("isBannerReady")) {
			TJLog ("Banner加载好啦");
			return true;
		} else {
			TJLog ("Banner没加载好");
			return false;
		}
	}

	public override bool IsInsertReady ()
	{
		TJLog ("检测插频状态");
		if (jo.CallStatic<bool> ("isInsertReady")) {
			TJLog ("插屏加载好啦");
			return true;
		} else {
			TJLog ("插屏没加载好");
			LoadInsert ();
			return false;
		}
	}

	#endregion

	public override void OnADCallback (string str)
	{
		TJLog ("广告回调: " + str);
		string[] datas = str.Split ('|');
		switch (datas [0]) {
		case "Banner":
			switch (datas [1]) {
			case "onADReceive":
				TJLog ("接收到Banner广告");
				break;
			case "onNoAD":
				TJLog ("无Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (false);
					bannerCallBack = null;
				}
				break;
			case "onADExposure":
				TJLog ("曝光Banner广告");
				if (bannerCallBack != null) {
					bannerCallBack (true);
					bannerCallBack = null;
				}
				break;
			default:
				break;
			}
			break;
		case "Insert":
			switch (datas [1]) {
			case "onADReceive":
				TJLog ("已收到插屏广告");
				break;
			case "onNoAD":
				TJLog ("无插屏广告:"+datas[2]);
				if (insertCallBack != null) {
					insertCallBack (false);
					insertCallBack = null;
				}
				break;
			case "onADExposure":
				TJLog ("曝光插屏广告");
				if (insertCallBack != null) {
					insertCallBack (true);
					insertCallBack = null;
				}
				break;
			default:
				break;
			}
			break;
		default:
			break;
		}
	}
}
